export default {
  namespaced: true,
  state: {
    bannerData: [], //banner数据
    typeData: sessionStorage.getItem("typeData")
      ? JSON.parse(sessionStorage.getItem("typeData"))
      : [], //分类数据 在拿数据之前先判断一下storage里面是否有值，有则将其转化成js对像没有的话置为空
  },
  getters: {
    getBanner(state) {
      return state.bannerData;
    },
    getTypeData(state) {
      return state.typeData;
    },
    getTitle(state) {
      return state.typeData.filter((item) => item.typelevel == 0);
    },
    recommedType(state) {
      let data = state.typeData.filter((item) => {
        return item.fatherid.includes(1) && item.typelevel === 1;
      });
      console.log(state.typeData);
      return data.length > 4 ? data.splice(0, 4) : data;
    },
  },
  mutations: {
    setBanner(state, payload) {
      //修改banner数据
      state.bannerData = payload.data;
    },
    setType(state, payload) {
      //利用storage持久存储数据
      sessionStorage.setItem("typeData", JSON.stringify(payload.data));
      state.typeData = payload.data;
      console.log(state.typeData);
    },
  },
  actions: {
    //设置banner的actions方法
    setBannerActions(context, payload) {
      context.commit({
        ...payload,
        type: "setBanner",
      });
    },
    //设置typeData数据的actions方法
    setTypeActions(context, payload) {
      context.commit({
        ...payload,
        type: "setType",
      });
    },
  },
};
